package com.wxl.common.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class APNMatchTools {

    private static Uri uri = Uri.parse("content://telephony/carriers");

    public static class APNNet {

        public static String CMWAP ="cmwap";

        public static String CMNET ="cmnet";
        // 中国联通3GWAP设置 中国联通3G因特网设置中国联通WAP设置 中国联通因特网设置
        // 3gwap 3gnet uniwapuninet

        public static String GWAP_3= "3gwap";

        public static String GNET_3= "3gnet";

        public static String UNIWAP= "uniwap";

        public static String UNINET= "uninet";
    }

    public static String matchAPN(String currentName) {
        if ("".equals(currentName) ||null == currentName) {
            return"";
        }
        currentName =currentName.toLowerCase();
        if(currentName.startsWith(APNNet.CMNET))
            return APNNet.CMNET;
        else if(currentName.startsWith(APNNet.CMWAP))
            return APNNet.CMWAP;
        else if(currentName.startsWith(APNNet.GNET_3))
            return APNNet.GNET_3;
        else if(currentName.startsWith(APNNet.GWAP_3))
            return APNNet.GWAP_3;
        else if(currentName.startsWith(APNNet.UNINET))
            return APNNet.UNINET;
        else if(currentName.startsWith(APNNet.UNIWAP))
            return APNNet.UNIWAP;
        else if(currentName.startsWith("default"))
            return"default";
        else
            return"";
        // returncurrentName.substring(0, currentName.length() -
        // SUFFIX.length());
    }

    public static boolean openAPN(Context context) {
        List<APN> list = getAPNList(context);
        for (APN apn : list) {
            ContentValues cv = new ContentValues();
            cv.put("apn", APNMatchTools.matchAPN(apn.apn));
            cv.put("type", APNMatchTools.matchAPN(apn.type));
            context.getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
        }
        return true;
    }

    public static boolean closeAPN(Context context) {
        List<APN> list = getAPNList(context);
        for (APN apn : list) {
            ContentValues cv = new ContentValues();
            cv.put("apn", APNMatchTools.matchAPN(apn.apn)+"mdev");
            cv.put("type", APNMatchTools.matchAPN(apn.type)+"mdev");
            context.getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
        }
        return true;
    }

    private static List<APN> getAPNList(Context context){
        String tag = "Main.getAPNList()";
        //current不为空表示可以使用的APN
        String  projection[] = {"_id,apn,type,current"};
        Cursor cr = context.getContentResolver().query(uri, projection, null, null, null);
        List<APN> list = new ArrayList<APN>();
        while(cr!=null && cr.moveToNext()){
            Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + "  " + cr.getString(cr.getColumnIndex("apn")) + "  " + cr.getString(cr.getColumnIndex("type"))+ "  " + cr.getString(cr.getColumnIndex("current")));
            APN a = new APN();
            a.id = cr.getString(cr.getColumnIndex("_id"));
            a.apn = cr.getString(cr.getColumnIndex("apn"));
            a.type = cr.getString(cr.getColumnIndex("type"));
            list.add(a);
        }
        if(cr!=null)
            cr.close();
        return list;
    }

    public static class APN {
        String id;
        String apn;
        String type;
    }
}
